package com.lykj.llw.box.ui.diafrg

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.lykj.lib_base.base.BaseDiaFrg
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.inter.OnConfirmClickListener
import com.lykj.llw.box.databinding.DiafrgConfirmBinding
import me.jessyan.autosize.utils.AutoSizeUtils

/**
 * ================================================
 * 确认弹窗
 * ================================================
 *
 * @author gosha
 * @date 2023/12/8-14:47
 */
class ConfirmDiaFrg : BaseDiaFrg() {
    lateinit var binding: DiafrgConfirmBinding

    private var mListener: OnConfirmClickListener? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DiafrgConfirmBinding.inflate(layoutInflater)
        initUI()
        initData()
        setListener()
        return binding.root
    }

    override fun onStart() {
        super.onStart()

        val window = dialog!!.window
        //设置窗口弹出动画
        window!!.setWindowAnimations(0)
        val layoutParams = window.attributes
        layoutParams.dimAmount = 0.75f
        layoutParams.gravity = Gravity.CENTER
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT

        if (null != dialog) {
            val dm = DisplayMetrics()
            requireActivity().windowManager.defaultDisplay.getMetrics(dm)
            dialog!!.setCancelable(false)
            dialog!!.setCanceledOnTouchOutside(false)
            dialog!!.window!!.setGravity(Gravity.CENTER)
            dialog!!.window!!.setLayout(
                AutoSizeUtils.dp2px(requireContext(), 262f),
                AutoSizeUtils.dp2px(requireContext(), 100f)
            )
        }
    }

    open fun setOnConfirmListener(listener: OnConfirmClickListener): ConfirmDiaFrg {
        this.mListener = listener
        return this
    }

    override fun initUI() {
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.tvBtnSure.clickWithTrigger {
            mListener?.onSure()
            dismissAllowingStateLoss()
        }

        binding.tvBtnCancel.clickWithTrigger {
            mListener?.onCancel()
            dismissAllowingStateLoss()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}